package com.nhimeye.admin.ui;

import java.util.HashSet;

import org.springframework.beans.factory.annotation.Autowired;

import com.nhimeye.admin.app.AbstractEntityView;
import com.nhimeye.admin.app.EntityEditor;
import com.nhimeye.admin.app.EntityProviderUtil;
import com.nhimeye.admin.app.H1;
import com.nhimeye.domain.Field;
import com.nhimeye.service.FieldService;
import com.vaadin.addon.jpacontainer.EntityItem;
import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.spring.roo.addon.annotations.RooVaadinEntityView;
import com.vaadin.ui.Button;
import com.vaadin.ui.Table;


public class FieldView extends AbstractEntityView<Field> {

	@Autowired
	FieldService fieldService;
	
	private JPAContainer<Field> tableContainer;
	

    @Override
    protected void configureTable(Table table) {
        table.setContainerDataSource(getTableContainer());
        table.setVisibleColumns(getTableColumns());

    }

	@Override
	protected HashSet<String> getIgnoreTableColumns() {
		return new HashSet<String>();
	}

	@Override
	protected Class<? extends Field> getEntityClass() {
		return Field.class;
	}

	@Override
	protected String getEntityName() {
		return "Field";
	}

	@Override
	public boolean isCreateAllowed() {
		 return true;
	}

	@Override
	protected boolean isUpdateAllowed() {
		 return true;
	}

	@Override
	protected boolean isDeleteAllowed() {
		 return true;
	}

	@Override
	protected boolean isNewEntity(Field entity) {
		return (entity != null && getIdForEntity(entity) == null);
	}

	@Override
	protected void deleteEntity(Field entity) {
		if (entity != null) {
			 fieldService.deleteField(entity);
	        }
	}

	@Override
	protected Field saveEntity(Field entity) {
		if (entity == null) {
            return null;
        }
     fieldService.saveField(entity);
         return entity;
	}

	@Override
	public Object getIdProperty() {
		return "id";
	}

	@Override
	public Object getVersionProperty() {
		 return "version";
	}

	@Override
	protected Container getTableContainer() {
		if (tableContainer == null) {
            JPAContainer<Field> container = new JPAContainer<Field>(Field.class);
            container.setEntityProvider(EntityProviderUtil.get().getEntityProvider(Field.class));
            tableContainer = container;
        }
        return tableContainer;
	}

	@Override
	protected Item getItemForEntity(Field entity) {
        Item item = getTable().getItem(entity.getId());
        if (item == null) {
            item = tableContainer.createEntityItem(entity);
        }
        return item;
	}

	@Override
	protected Field getEntityForItem(Item item) {
		if (item instanceof EntityItem && ((EntityItem) item).getEntity() instanceof Field) {
            return (Field) ((EntityItem) item).getEntity();
        } else {
            return null;
        }
	}

	@Override
	public Object getIdForEntity(Field entity) {
		return entity != null ? entity.getId() : null;
	}

	@Override
	protected Field createEntityInstance() {
		return new Field();
	}

	@Override
	protected void configureButton(Button b) {
		b.setCaption("+ Add Fields");

		
	}

	@Override
	protected void configureCaption(H1 title) {
		title.setCaption("Fields");
	}

	@Override
	protected EntityEditor createForm() {
		return new FieldForm();
	}

}
